package com.smalls.newvideotwo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.smalls.newvideotwo.R;
import com.smalls.newvideotwo.mvp.bean.StreamInfo;
import com.smalls.newvideotwo.util.LogUtil;
import com.smalls.newvideotwo.utils.Constant;
import com.smalls.newvideotwo.utils.MeasureUtil;

import java.util.ArrayList;
import java.util.List;

import static com.smalls.newvideotwo.utils.Tools.setGridLayoutManagerLayoutParam;

/**
 * Created by hs on 2018/3/14.
 */

public class NumberListAdapter extends RecyclerView.Adapter<NumberListAdapter.SerialHolder> {
    private static String TAG = Constant.baddcoolio2tag? Constant.TAGPREFIXSTR + NumberListAdapter.class.getSimpleName():NumberListAdapter.class.getSimpleName();
    private List<StreamInfo> mSerialList = null;
    private Context mContext = null;
    private TextView selectTextView = null;
    private View.OnClickListener mOnClickListener = null;
    private View.OnKeyListener mOnKeyListener=null;
    private View.OnFocusChangeListener mOnFocusListener= null;
    private View mSerialSelectedView = null;

    public NumberListAdapter(List<StreamInfo> paramArrayList) {
       mSerialList = paramArrayList;
    }

    @Override
    public NumberListAdapter.SerialHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.detail_serial_item, parent, false);
        SerialHolder viewHolder = new SerialHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final NumberListAdapter.SerialHolder holder, int position) {
        ///LogUtil.d(TAG,"onBindViewHolder position="+position);
        SerialHolder viewHolder = (SerialHolder) holder;
        if (mSerialList != null && position < mSerialList.size()) {
            viewHolder.mSerial.setText(mSerialList.get(position).getSequence()+"");
            setGridLayoutManagerLayoutParam(viewHolder.mSerial,-1,96,-1,-1);
            MeasureUtil.setTextSize(viewHolder.mSerial, 21);
            viewHolder.mSerial.setTag(position);
            viewHolder.mSerial.setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    if (event.getAction() == KeyEvent.ACTION_DOWN) {
                        int position = (Integer) v.getTag();
                        switch (keyCode) {
                            case KeyEvent.KEYCODE_DPAD_UP:
                                mSerialSelectedView = v;
                                if (position / 10 == 0) {
                                    return true;
                                }
                                break;
                            default:
                                break;
                        }
                    }
                    return false;
                }
            });
            viewHolder.mSerial.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean b) {
                    LogUtil.i(TAG,"mSerial onFocusChange b = "+b+" pos="+view.getTag());
                }
            });
            if(mOnClickListener!=null) {
                viewHolder.mSerial.setOnClickListener(mOnClickListener);
            } else {
                viewHolder.mSerial.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mSerialSelectedView = v;
                        int position = (Integer) v.getTag();
                        int serial = mSerialList.get(position).getSequence();
                        Toast.makeText(mContext, "选中" + serial + "集", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }
    }

    @Override
    public int getItemCount() {
        if(mSerialList!=null) {
            //LogUtil.i(TAG,"getItemCount="+mSerialList.size());
            return mSerialList.size();
        }
        return 0;
    }

    public void setOnRightKeyListener(View.OnKeyListener listener) {
        mOnKeyListener = listener;
    }

    public void setOnClickListenter( View.OnClickListener listenter) {
        mOnClickListener = listenter;
    }

    public void setOnFocusListener(View.OnFocusChangeListener listener) {
        mOnFocusListener = listener;
    }

    public class SerialHolder extends RecyclerView.ViewHolder {
        public Button mSerial = null;

        public SerialHolder(View itemView) {
            super(itemView);

            if (itemView != null) {
                mSerial = (Button) itemView.findViewById(R.id.item_title);
            }
        }
    }
}
